import UserService from '../services/user.js';

const UserController = {

  async createUser(req, res) {
    console.log('register:',req.body);
    const data = await UserService.createUser(req.body);
    return res.status(200).json(data);
  },

  async login(req, res) {
    console.log(req.body);
    const data = await UserService.login(req.body);
    return res.status(200).json(data);
  },

  async update(req, res) {
    const data = await UserService.update(req.body);
    return res.status(200).json(data);
  },

  async getBindingList(req, res){
    try{
      const userId = req.params.userId;
      const page = req.query.page || -1;
      const pageSize = req.query.pageSize || -1;
      let result;
      if( page === -1 || pageSize === -1){
        result = await UserService.getBindingList(userId);
      }else{
        result = await UserService.getBindingListPages(userId,page,pageSize);
      }


      return res.status(200).json(result);
    }catch(error){
     return res.status(500).json(error); 
    }
  },
  async binding(req, res){
    const {userId,bindedUserId,bindedUserPhone,relation} = req.body;
    try{
      let result;
      if(!bindedUserId && bindedUserPhone){
        console.log('bindedUserPhone',bindedUserPhone);
        
         result = await UserService.bindingByPhone(userId,bindedUserPhone,relation);
      }else{
        console.log('bindedUserId',bindedUserId);
         result = await UserService.binding(userId,bindedUserId,relation);
      }
      return res.status(200).json(result);
    }catch(error){
      return res.status(500).json(error);
    }


  },
  async unbinding(req, res){
    const {userId,bindedUserId} = req.body;
    try{
      const result = await UserService.unbinding(userId,bindedUserId);
      return res.status(200).json(result);
    }catch(error){
      return res.status(500).json(error);
    }
  }


}


export default UserController;
